package com.ruoyi.system.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.MedicineStatus;
import com.ruoyi.system.domain.vo.SysMedicinesVo;
import com.ruoyi.system.service.ISysJashuService;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysMedicines;
import com.ruoyi.system.service.ISysMedicinesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 药品Controller
 * 
 * @author jiashu
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/system/medicine")
public class SysMedicinesController extends BaseController
{
    @Autowired
    private ISysMedicinesService sysMedicinesService;

    @Autowired
    private ISysJashuService sysJashuService;

    /**
     * 查询药品列表
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMedicines sysMedicines)
    {
        sysMedicines = sysJashuService.getSysMedicinesSelectParam(sysMedicines);
        startPage();
        List<SysMedicines> list = sysMedicinesService.selectSysMedicinesList(sysMedicines);
        return getDataTable(sysJashuService.getSysMedicinesVoList(list));
    }

    /**
     * 导出药品列表
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:export')")
    @Log(title = "药品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMedicines sysMedicines)
    {
        List<SysMedicines> list = sysMedicinesService.selectSysMedicinesList(sysMedicines);
        List<SysMedicinesVo> sysMedicinesVoList = sysJashuService.getSysMedicinesVoList(list);
        ExcelUtil<SysMedicinesVo> util = new ExcelUtil<SysMedicinesVo>(SysMedicinesVo.class);
        util.exportExcel(response, sysMedicinesVoList, "药品数据");
    }

    /**
     * 获取药品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:query')")
    @GetMapping(value = "/{medicineId}")
    public AjaxResult getInfo(@PathVariable("medicineId") Long medicineId)
    {
        return success(sysMedicinesService.selectSysMedicinesByMedicineId(medicineId));
    }

    /**
     * 新增药品
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:add')")
    @Log(title = "药品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMedicinesVo sysMedicinesVo) throws IOException {
        SysMedicines vo = new SysMedicines();
        vo.setMedicineName(sysMedicinesVo.getMedicineName());
        List<SysMedicines> sysMedicines = sysMedicinesService.selectSysMedicinesList(vo);
        if(!sysMedicines.isEmpty()){
            return AjaxResult.error("该药品名称已存在！");
        }


        sysMedicinesVo.setCreateBy(getUsername());
        sysMedicinesVo.setCreateTime(DateUtils.getNowDate());
        return toAjax(sysMedicinesService.insertSysMedicines(sysMedicinesVo));
    }


    @PreAuthorize("@ss.hasPermi('system:medicine:upimg')")
    @Log(title = "药品图片上传", businessType = BusinessType.IMPORT)
    @PostMapping("/importImg/{medicineId}")
    public AjaxResult importImg(@PathVariable Long medicineId, @RequestParam("file") MultipartFile file){
        if (!file.isEmpty())
        {
            SysMedicines sysMedicines = sysMedicinesService.selectSysMedicinesByMedicineId(medicineId);
            if(ObjectUtil.isNotNull(sysMedicines)){
                sysMedicinesService.importImg(sysMedicines, file);
                return success("图片上传成功");
            }
        }
        return error("上传图片异常，请联系管理员");
    }




    /**
     * 修改药品
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:edit')")
    @Log(title = "药品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMedicinesVo sysMedicinesVo) throws IOException {
//        Long medicineId = sysMedicinesVo.getMedicineId();
//        SysMedicines sysMedicinesDB = sysMedicinesService.selectSysMedicinesByMedicineId(medicineId);
//        String status = sysMedicinesDB.getStatus();
//        if(MedicineStatus.Audited.getKey().equals(status)){
//            return AjaxResult.error("该药品已通过审核，无法修改！");
//        }
        // 校验修改名称
//
        if (!sysMedicinesService.checkUpdateMedicinesName(sysMedicinesVo)) {
            return AjaxResult.error("药品名称重复");
        }
        sysMedicinesVo.setStatus("0");
        sysMedicinesVo.setUpdateBy(getUsername());
        sysMedicinesVo.setUpdateTime(DateUtils.getNowDate());
        return toAjax(sysMedicinesService.updateSysMedicines(sysMedicinesVo));
    }

    /**
     * 删除药品
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:remove')")
    @Log(title = "药品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{medicineIds}")
    public AjaxResult remove(@PathVariable Long[] medicineIds)
    {
        return toAjax(sysMedicinesService.deleteSysMedicinesByMedicineIds(medicineIds));
    }


    /**
     * 审核药品
     */
    @PreAuthorize("@ss.hasPermi('system:medicine:examine')")
    @Log(title = "药品审核", businessType = BusinessType.UPDATE)
    @PutMapping("/examine")
    public AjaxResult examine(@RequestBody SysMedicines sysMedicines)
    {
        Long medicineId = sysMedicines.getMedicineId();
        SysMedicines sysMedicines1 = sysMedicinesService.selectSysMedicinesByMedicineId(medicineId);
        String status = sysMedicines1.getStatus();
        if("1".equals(status)){
            return AjaxResult.error("该药品已通过审核");
        }
        return toAjax(sysMedicinesService.updateSysMedicines(sysMedicines));
    }


}
